package baidu;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class two {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int T = scan.nextInt();

        for (int i = 0; i < T; i++) {
            int n =scan.nextInt();
            int a[] = new int[n];
            int m = scan.nextInt();
            int count = 0;
            HashMap<Integer,Integer> hashMap = new HashMap<>();
            for (int j = 0; j < m; j++) {
                int k = scan.nextInt();
                for (int z = 0; z < k; z++) {
                    int l = scan.nextInt();
                    int r = scan.nextInt();
                    if(j ==0){
                        hashMap.put(l,r);
                    }
                    if(j==m-1){
                        if(hashMap.containsKey(l)){
                            for (int o = l; o < Math.max(r,hashMap.get(l))-1; o++) {
                                count++;
                                a[count] = o;
                            }
                        }
                        if(hashMap.containsKey(r)){
                            count++;
                            a[count] = r;
                        }
                        /*if(hashMap.containsValue(r)){
                            int val = 0;
                            for(Map.Entry<Integer,Integer> hash:hashMap.entrySet()){
                                if(hash.getValue()==r){
                                    val = hash.getKey();
                                }
                            }
                            for (int o = Math.max(l,val)+1; o < r; o++) {
                                a[count] = o;
                                count++;
                            }
                        }*/
                    }else{
                        if(hashMap.containsKey(l)){
                            hashMap.put(l,Math.min(r,hashMap.get(l)));
                        }else if(hashMap.containsValue(r)){
                            int val = 0;
                            for(Map.Entry<Integer,Integer> hash:hashMap.entrySet()){
                                if(hash.getValue()==r){
                                    val = hash.getKey();
                                }
                            }
                            hashMap.put(l,Math.max(l,val));
                        }
                }
                }


            }
            System.out.println(count);
            Arrays.sort(a);
            for(int aa:a){
                if(aa!=0){
                    System.out.print(aa + " ");
                }
            }
            System.out.println();
        }
    }
}
